import router from './router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import store from '@/store'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login','/invites','/enterSelf','/invitesmore'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()
  const hasToken = getToken()
  if (whiteList.indexOf(to.fullPath) !== -1 || whiteList.indexOf(to.fullPath.split("?")[0]) !== -1) {
    next()
    return
  }
  if (hasToken) {
    // store.dispatch('dict/getDict')
    if (!store.getters.menuList.length) {
      await store.dispatch('user/getMenu')
    }
    if (!store.getters.deptList.length) {
      await store.dispatch('dict/getDict')
    }

    next()
  } else {
    next({
      path: '/login'
    })
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
